extends Node2D
var peer : ENetMultiplayerPeer
var ids : Array[int]
var players : Dictionary = {}
var player_group

func _ready() -> void:
	peer = ENetMultiplayerPeer.new()
	var result = peer.create_server(7001)
	if not is_same(result,OK) :
		printerr("服务器启动失败，错误码：",result)
		return
	print("服务器启动成功>>>>>>>>>>>")
	multiplayer.multiplayer_peer = peer
	multiplayer.peer_connected.connect(_on_peer_connected)
	multiplayer.peer_disconnected.connect(_on_peer_disconnected)

func _on_peer_connected(con_id : int) :
	print("玩家连接：",con_id)
	#检测redis否存在用户id
	ids.append(con_id)
	for id in ids :
		player_group.rpc("_add_player",con_id)

func _on_peer_disconnected(discon_id : int) :
	print("玩家断连：",discon_id)
	for id in ids :
		player_group.rpc("_remove_player",discon_id)
	ids.erase(discon_id)
